/**
 * @param {number} n
 * @return {number[][]}
 */
 var generateMatrix = function (n) {
    let left = 0
    let top = 0
    let right = n - 1
    let bottom = n - 1
    let total = n * n
    let res = []
    for (let i = 0; i < n; i++) {
      res[i] = []
    }
    let count = 0
    while (count < total) {
      for (let i = left; i <= right; i++) { // 从左到右
        res[left][i] = ++count
      }
      top++
      for (let i = top; i <= bottom; i++) { // 从上到下
        res[i][bottom] = ++count
      }
      right--
      for (let i = right; i >= left; i--) { // 从右到左
        res[bottom][i] = ++count
      }
      bottom--
      for (let i = bottom; i >= top; i--) { // 从下到上
        res[i][left] = ++count
      }
      left++
    }
    return res
  };